package cn.ld.fj.service.log;

import cn.ld.fj.dao.account.UserDao;
import cn.ld.fj.dao.log.OperationlogDao;
import cn.ld.fj.dao.order.MealOrderDao;
import cn.ld.fj.entity.MealOrder;
import cn.ld.fj.entity.OperationLog;
import cn.ld.fj.entity.account.User;
import net.esoar.modules.orm.Page;
import net.esoar.modules.orm.PropertyFilter;
import net.esoar.modules.security.springsecurity.SpringSecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 安全相关实体的管理类, 包括用户,角色,资源与授权类.
 *
 * @author fly
 */
//Spring Bean的标识.
@Component
//默认将类中的所有函数纳入事务管理.
@Transactional
public class OperationLogManager {

    private static Logger logger = LoggerFactory.getLogger(OperationLogManager.class);
    @Autowired
    private OperationlogDao operationlogDao;
    @Autowired
    private UserDao userDao;


    @Transactional(readOnly = true)
    public OperationLog getOne(Long id) {
        return operationlogDao.get(id);
    }

    public void save(OperationLog entity) {
        operationlogDao.save(entity);
    }


    public void saveLog(String content) {
        OperationLog operationLog = new OperationLog();


        String loginName = SpringSecurityUtils.getCurrentUserName();
        User user = userDao.findUniqueBy("loginName", loginName);
        if (user != null) {
            operationLog.setOperationId(user.getId());
        } else {
            operationLog.setOperationId(0l);
        }
        operationLog.setCreateDate(new Date());
        operationLog.setContent(content);
        operationlogDao.save(operationLog);
    }


    public Page<OperationLog> getPage(Page<OperationLog> page, List<PropertyFilter> filters) {
        return operationlogDao.findPage(page, filters);
    }

    public void delete(Long id) {
        operationlogDao.delete(id);
    }

}
